package llvmir.value.instructions;

import llvmir.value.Value;
import llvmir.value.structure.BasicBlock;
import llvmir.value.structure.Instruction;

import java.util.HashMap;
import java.util.Map;

import llvmir.type.Integer32Type;

public class BinaryOp extends Instruction {
    private String op;

    public BinaryOp(Value left, Value right, String op, BasicBlock basicBlock) {
        super(null, new Integer32Type(), basicBlock);
        operands.add(left);
        operands.add(right);
        this.op = op;
    }

    public String getOp() {
        return op;
    }

    // 可能出现的操作符有：+,-,*,/,%，构建一个map存储这些操作符对应的字符串
    Map<String, String> opMap = new HashMap<String, String>() {
        {
            put("+", "add");
            put("-", "sub");
            put("*", "mul");
            put("/", "sdiv");
            put("%", "srem");
            put("<<", "shl");
            put(">>", "ashr");
        }
    };


    @Override
    public String toString() {
        return name + " = " + opMap.get(op) + " " + this.getType() + " " + operands.get(0).getName() + ", "
                + operands.get(1).getName();
    }
}
